<?php

function copycropped($f1, $f2, $w, $h=NULL, $q=100)
{
  $h = $h ? $h : $w;
  $jpeg = array("jpeg", "JPEG", "jpg", "JPG");
  $gif = array("gif", "GIF");
  $png = array("PNG", "png");

  $type = explode(".", $f1);
  $type = $type[count($type)-1];

  $type2 = explode(".", $f2);
  $type2 = $type2[count($type2)-1];

  if (in_array($type, $jpeg))
  $im_src = imagecreatefromjpeg($f1);
  if (in_array($type, $gif))
  $im_src = imagecreatefromgif($f1);
  if (in_array($type, $png))
  $im_src = imagecreatefrompng($f1);
  if (!empty($im_src))
  {
    $w_src = imagesx($im_src);
    $h_src = imagesy($im_src);
    $w_src2 = $w_src;
    $h_src2 = $h_src;
    $ratio1 = $h_src/$h;
    $ratio2 = $w_src/$w;
    $h_dest = $h;
    $w_dest = $w;
    if ( $ratio1 != 1 || $ratio2 != 1) {
      if ( $ratio1 > $ratio2 )
      {
        $h_src = round($ratio2*$h_dest);
        $h = round(($h_src2-$h_src)/2);
        $im_dest = imagecreatetruecolor($w_dest,$h_dest);
        imagecopyresampled($im_dest, $im_src, 0, 0, 0, $h, $w_dest, $h_dest, $w_src, $h_src);
      }
      else
      {
        $w_src = round($ratio1*$w_dest);
        $w = round(($w_src2-$w_src)/2);
        $im_dest = imagecreatetruecolor($w_dest,$h_dest);
        imagecopyresampled($im_dest, $im_src, 0, 0, $w, 0, $w_dest, $h_dest, $w_src, $h_src);
      }
      if (in_array($type2, $jpeg))
      imagejpeg($im_dest, $f2, $q);
      if (in_array($type2, $gif))
      imagegif($im_dest, $f2);
      if (in_array($type2, $png))
      imagepng($im_dest, $f2, $q);
    }
  }
}
  

  function copyresizedw($f1, $f2, $w, $q=100)
  {
    $jpeg = array("jpeg", "JPEG", "jpg", "JPG");
    $gif = array("gif", "GIF");
    $png = array("PNG", "png");

    $type = explode(".", $f1);
    $type = $type[count($type)-1];
    
    $type2 = explode(".", $f2);
    $type2 = $type2[count($type2)-1];
    
    if (in_array($type, $jpeg))
      $im_src = imagecreatefromjpeg($f1);
    if (in_array($type, $gif))
      $im_src = imagecreatefromgif($f1);
    if (in_array($type, $png))
      $im_src = imagecreatefrompng($f1);
    if (!empty($im_src))
    {
      $w_src = imagesx($im_src);
      $h_src = imagesy($im_src);
      $ratio = $w_src/$w;
      if ( $ratio != 1 ) {
        $w_dest = round($w_src/$ratio);
        $h_dest = round($h_src/$ratio);
        $im_dest = imagecreatetruecolor($w_dest,$h_dest);
        imagecopyresampled($im_dest, $im_src, 0, 0, 0, 0, $w_dest, $h_dest, $w_src, $h_src);
        if (in_array($type2, $jpeg))
          imagejpeg($im_dest, $f2, $q);
        if (in_array($type2, $gif))
          imagegif($im_dest, $f2);
        if (in_array($type2, $png))
          imagepng($im_dest, $f2, $q);
      }
    }
  }
  
 
class upload {
  public $file, $fullpath, $type, $filename, $name, $error, $http_link;
  public static $folder = 'upload/';
  public static $table = 'upload';

  function __construct($f)
  {
    $this->file = $_FILES[$f];
    $this->name = $this->file["name"];
    $this->filename = translit( str_replace( ' ', '_', $this->file["name"]) );
    $this->type = $this->file['type'];
  }


  function save( $mime="*/*", $replace = false ) {
    if (!empty($this->file["tmp_name"]))
    {
      if ( mask( $this->file['type'], $mime ))
      {
        if ( file_exists(self::$folder . $this->filename) && !$replace )
          $this->filename = date('Ymd-His_') . $this->filename;
        if (@move_uploaded_file($this->file["tmp_name"], self::$folder.$this->filename)) {
          if ( mask( $this->file['type'], 'image/*' ) ) {
            copyresizedw( self::$folder.$this->filename, self::$folder.$this->filename, 800 );
            copycropped( self::$folder.$this->filename, self::$folder.'preview_'.$this->filename, 160, 105 );
          }
          $sql = "INSERT INTO `:1` ( `file` , `name` , `date` , `author` , `mime` , `preview` )
                  VALUES ( ':2', ':3', ".time().", ':4', ':5', 'preview_:2' )";
          db::q( $sql, self::$table, $this->filename, $this->name, system::$user->id, $this->file['type'] );
          $this->http_link = 'http://'.page::$domain.'/'.self::$folder.$this->filename;
          $this->error = 0;
        } else {
          $this->error = 1;
        }
      }
      else
      $this->error = 2;
    }
    else
    $this->error = 3;
  }

  function error()
  {
    switch ($this->error) {
      case 0:
        return "Файл успешно загружен.";
      case 1:
        return "Файл _не_ сохранен.";
      case 2:
        return "Неверный тип файла.";
      case 3:
        return "Файл не загружен.";
    }
  }
}
